package com.example.ktpspringboot.controller;

import com.example.ktpspringboot.entity.HomeWork;
import com.example.ktpspringboot.service.impl.HomeWorkServiceImpl;
import com.example.ktpspringboot.utils.SnowflakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class HomeWorkController {

    @Autowired
    private HomeWorkServiceImpl homeWorkServiceImpl;

    @Autowired
    private SnowflakeUtil snowflakeUtil;

    @RequestMapping("/creatHomeWork")
    public String addHomeWork(@RequestBody Map<String, Object> json) {
        String code = (String) json.get("code");
        String workTitle = (String) json.get("workTitle");
        String workType = (String) json.get("workType");
        String workOther = (String) json.get("workOther");
        String activity = (String) json.get("activity");
        String knowledge = (String) json.get("knowledge");
        String apply = (String) json.get("apply");
        String page = (String) json.get("page");
        String issueTime = (String) json.get("issueTime");
        String overTime = (String) json.get("overTime");
        int workFormat = (int) json.get("workFormat");
        String sore = (String) json.get("sore");
        String duplicateCheck = (String) json.get("duplicateCheck");
        String restrictFormat = (String) json.get("restrictFormat");
        String repetitiveRate = (String) json.get("repetitiveRate");
        String issueState = (String) json.get("issueState");
        HomeWork homeWork = new HomeWork();
        homeWork.setWorkTitle(workTitle);
        homeWork.setWorkType(workType);
        homeWork.setWorkOther(workOther);
        homeWork.setActivity(activity);
        homeWork.setKnowledge(knowledge);
        homeWork.setApply(apply);
        homeWork.setPage(page);
        homeWork.setIssueTime(issueTime);
        homeWork.setOverTime(overTime);
        homeWork.setWorkFormat(homeWorkServiceImpl.workFormat(workFormat, restrictFormat));
        homeWork.setSore(sore);
        homeWork.setDuplicateCheck(duplicateCheck);
        homeWork.setRepetitiveRate(repetitiveRate);
        homeWork.setIssueState(issueState);
        homeWork.setUuid(String.valueOf(snowflakeUtil.generateId()));
        return homeWorkServiceImpl.insertHomework(homeWork, code);
    }

    @RequestMapping("/getHomeWork")
    public List<HomeWork> getHomework(@RequestBody Map<String, Object> json) {
        String code = (String) json.get("code");
        return homeWorkServiceImpl.getHomeWork(code);
    }
}
